package com.czy.owner.adapter;


import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.MyPackageEntity;
import com.czy.owner.entity.PackageCardProjectModel;
import com.czy.owner.entity.PackageSelectModel;
import com.czy.owner.entity.SelectBespokeListModel;
import com.czy.owner.ui.packagecardproject.MyPackageCardDetailActivity;
import com.czy.owner.ui.packagecardproject.PackageCardDetailActivity;
import com.czy.owner.ui.packagecardproject.PackageCardProjectActivity;
import com.czy.owner.utils.DecimalUtil;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.StringUtils;
import com.czy.owner.utils.TimeUtils;
import com.czy.owner.widget.shopbutton.AnimShopButton;
import com.czy.owner.widget.shopbutton.IOnAddDelListener;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by wangyang on 2017/11/20 0014.
 */

public class PackageCardProjectAdapter2 extends RecyclerArrayAdapter<PackageCardProjectModel> {
    private Context ctx;
    private int themeColor = Color.parseColor("#09c0c7");
    private LinkedHashMap<Integer, PackageCardProjectModel> map = new LinkedHashMap<>();
    private List<PackageCardProjectModel> selectedList = new ArrayList<>();
    private ArrayList<Integer> selectData = new ArrayList<>();
    public PackageCardProjectAdapter2(Context context) {
        super(context);
        this.ctx = context;

    }


    public List<PackageCardProjectModel> getSelectedList() {
        selectedList.clear();
        for (Map.Entry<Integer, PackageCardProjectModel> entry : map.entrySet()) {
            selectedList.add(entry.getValue());
        }
        return selectedList;
    }


    public void delMap() {
        selectedList.clear();
        map.clear();
    }

    public void setSelectedList(List<PackageCardProjectModel> selectedList) {
        map.clear();
        for (PackageCardProjectModel packageCardProjectModel : selectedList) {
            map.put(packageCardProjectModel.getServicePackageId(), packageCardProjectModel);
        }
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new PackageCardProjectViewHolder(parent, ctx);
    }

    class PackageCardProjectViewHolder extends BaseViewHolder<PackageCardProjectModel> {
        private TextView tv_package_name;
        private CheckBox cb_store_package;
        private EasyRecyclerView easy_recycleview_sub;
        private TextView tv_old_price;
        private TextView tv_new_price;
        private TextView tv_give_amount;
        private TextView tv_use_time;
        private AnimShopButton btn_anmi_shop;
        private Context ctx;
        private MyPackageSubAdapter myPackageSubAdapter;
        private LinearLayout ll_give_amount;

        public PackageCardProjectViewHolder(ViewGroup parent, Context ctx) {
            super(parent, R.layout.item_store_package);
            tv_package_name = $(R.id.tv_package_name);
            tv_use_time = $(R.id.tv_use_time);
            tv_old_price = $(R.id.tv_old_price);
            tv_new_price = $(R.id.tv_new_price);
            cb_store_package = $(R.id.cb_store_package);
            easy_recycleview_sub = $(R.id.easy_recycleview_sub);
            btn_anmi_shop = $(R.id.btn_anmi_shop);
            ll_give_amount = $(R.id.ll_give_amount);
            tv_give_amount = $(R.id.tv_give_amount);

            this.ctx = ctx;
        }

        @Override
        public void setData(final PackageCardProjectModel model) {
            cb_store_package.setChecked(map.containsKey(model.getServicePackageId()));
            cb_store_package.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (map.containsKey(model.getServicePackageId())) {
                        map.remove(model.getServicePackageId());
                        if(selectListener!=null){
                            selectListener.onSelectChange();
                        }

                    } else {
                        map.put(model.getServicePackageId(), model);
                        if(selectListener!=null){
                            selectListener.onSelectChange();
                        }
                    }


                }
            });
            if (model.getGiveAmount() > 0) {
                ll_give_amount.setVisibility(View.VISIBLE);
                tv_give_amount.setText("¥" + DecimalUtil.formatNum(model.getGiveAmount() + ""));
            } else {
                ll_give_amount.setVisibility(View.GONE);

            }


//            model.setCurrentCount(1);
            MyLog.e("yang", "model.getCurrentCount()==" + model.getCurrentCount());
            btn_anmi_shop.setMaxCount(model.getSingleUsrMaxCount());
            if (model.getCurrentCount() != 1) {
                btn_anmi_shop.setCount(model.getCurrentCount());
            } else {
                btn_anmi_shop.setCount(1);
            }

            btn_anmi_shop.setOnAddDelListener(new IOnAddDelListener() {
                @Override
                public void onAddSuccess(int count) {
                    MyLog.e("yang", "onAddSuccess===" + count);
                    model.setCurrentCount(count);
                    if (cb_store_package.isChecked()) {
                        map.put(model.getServicePackageId(), model);
                    }

                    if(selectListener!=null){
                        selectListener.onSelectChange();
                    }
                }

                @Override
                public void onAddFailed(int count, FailType failType) {
                    if (failType == FailType.COUNT_MAX)
                        PhoneUtils.ShowToastMessage(ctx, "不能超出最大购买数量");

                }

                @Override
                public void onDelSuccess(int count) {
                    MyLog.e("yang", "onDelSuccess===" + count);
                    model.setCurrentCount(count);
                    if (cb_store_package.isChecked()) {
                        map.put(model.getServicePackageId(), model);
                    }
                    if(selectListener!=null){
                        selectListener.onSelectChange();
                    }

                }

                @Override
                public void onDelFaild(int count, FailType failType) {

                }
            });


            easy_recycleview_sub.setLayoutManager(new LinearLayoutManager(ctx));
            easy_recycleview_sub.setAdapter(myPackageSubAdapter = new MyPackageSubAdapter(ctx));

            List<PackageCardProjectModel.ItemListBean> mList = new ArrayList<>();
            if (model.getItemList() != null && model.getItemList().size() != 0) {
                for (int i = 0; i < model.getItemList().size(); i++) {
                    if (i < 5) {
                        PackageCardProjectModel.ItemListBean subPackageModel = model.getItemList().get(i);
                        mList.add(subPackageModel);
                    } else {
                        break;
                    }
                }
            }
            myPackageSubAdapter.addAll(mList);
            //myPackageSubAdapter.addAll(model.getItemList());
            myPackageSubAdapter.setOnItemClickListener(new OnItemClickListener<PackageCardProjectModel.ItemListBean>() {
                @Override
                public void onItemClick(View view, int position, PackageCardProjectModel.ItemListBean mSubData) {
                    if (PhoneUtils.isNetworkConnected(ctx)) {
                        Intent ti = new Intent(ctx, PackageCardDetailActivity.class);
                        ti.putExtra("servicePackageId", model.getServicePackageId());
                        ctx.startActivity(ti);
                    }
                }
            });

            int validityType = model.getValidityType();//0>>>永久有效;1>>>固定时长;2>>>固定时间
            int fixTimeType = model.getFixTimeType();//0>>>>>月;1>>>>年
            long startTime = model.getStartTime();
            long endTime = model.getEndTime();

            if (validityType == 0) {
                tv_use_time.setText("有效期: 永久有效");
            } else if (validityType == 1) {
                if (fixTimeType == 0) {
                    tv_use_time.setText("有效期: "+model.getFixTimeValue() + "个月有效");
                } else if (fixTimeType == 1) {
                    tv_use_time.setText("有效期: "+model.getFixTimeValue() + "年有效");
                }
            } else if (validityType == 2) {
                tv_use_time.setText("有效期: "+TimeUtils.formatTimeDifference4(startTime) + "-" + TimeUtils.formatTimeDifference4(endTime) + "有效");
            }
            tv_package_name.setText(model.getPackageName());
            tv_old_price.setText("原价￥" + String.format("%.2f", model.getOriginalPrice()));
            tv_new_price.setText(String.format("%.2f", model.getPrice()));
            tv_old_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);  // 设置中划线并加清晰

        }
    }


    class MyPackageSubAdapter extends RecyclerArrayAdapter<PackageCardProjectModel.ItemListBean> {
        public MyPackageSubAdapter(Context context) {
            super(context);

        }

        @Override
        public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyPackageSubAdapter.MyPackageSubViewHolder(parent);
        }

        class MyPackageSubViewHolder extends BaseViewHolder<PackageCardProjectModel.ItemListBean> {
            private TextView tv_name;
            private TextView tv_sum_number;
            private TextView tv_used_number;

            public MyPackageSubViewHolder(ViewGroup parent) {
                super(parent, R.layout.item_my_package_sub);
                tv_name = $(R.id.tv_name);
                tv_sum_number = $(R.id.tv_sum_number);
                tv_used_number = $(R.id.tv_used_number);

            }

            @Override
            public void setData(final PackageCardProjectModel.ItemListBean model) {
                tv_name.setText(model.getItemName());
                tv_sum_number.setGravity(Gravity.RIGHT);
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tv_sum_number.getLayoutParams();
                lp.setMargins(0, 0, PhoneUtils.dip2px(tv_sum_number.getContext(), 30), 0);
                tv_sum_number.setLayoutParams(lp);
                if (!model.isUnlimitedCount()) {
                    tv_sum_number.setText("不限次");
                } else {
                    tv_sum_number.setText("x" + model.getCount());
                }
                tv_sum_number.setTextColor(themeColor);
                tv_used_number.setVisibility(View.GONE);
            }
        }
    }
    private SelectListener selectListener;
    public interface SelectListener {
        void onSelectChange();
    }

    public void setSelectListener(SelectListener selectListener) {
        this.selectListener = selectListener;
    }
}
